<?php

namespace Management\Controller;

use Think\Controller;

use Myself\FileSystem;

class CarController extends BaseController {



	protected $m = NULL;

	protected $m_class = NULL;

	protected $m_config = NULL;

    protected $statusArr=array("未认证","已认证");

	

	public function _initialize(){

		parent::_initialize();

		$this->m = M('car');

		

	}

		

	

	

    public function index(){

		header('Content-Type:text/html;charset=utf-8');

		$where=[];

		//-----------------搜索-------------------
		if(I('driver_id')!=''){
			$where['driver_id'] = I('driver_id');
			$this->assign('driver_id',I('driver_id'));
		}
		if(I('cartype_id')!=''){
			$where['cartype_id'] = I('cartype_id');
			$this->assign('cartype_id',I('cartype_id'));
		}
		if(I('car_no')!=''){
			$where['car_no'] = array('like','%'.I('car_no').'%');
			$this->assign('car_no',I('car_no'));
		}


		//-------------------------搜索end-----------------

		$pageno = I('p',1,'int');

		$pageCount = 10;

		$list = $this->m->where($where)->page($pageno.','.$pageCount)->select();
		if(!empty($list)){
			foreach ($list as $k => $v) {
				$list[$k]['cartype_name'] = M('cartype')->where(['id'=>$v['cartype_id']])->getField('title');
				$list[$k]['driver_name'] = M('driver')->where(['id'=>$v['driver_id']])->getField('realname');
				if(!empty($v['worker_id'])){
					$worker = explode(',',$v['worker_id']);
					$arr =  M('driver')->field(['realname','nickname'])->where(['id'=>['in',$worker]])->select();
					foreach ($arr as $kk => $vv) {
						$worker_name .= $vv['realname'].'('.$vv['nickname'].')/';
					}
					$list[$k]['worker_name'] =$worker_name;
				}
			}
		}
		$this->assign('list',$list);// 赋值数据集

		$count = $this->m->where($where)->count();// 查询满足要求的总记录数

		$this->assign('count',$count);// 赋值分页输出	

		$Page = new \Think\Page($count,$pageCount);// 实例化分页类 传入总记录数和每页显示的记录数

		$show = $Page->showManage();// 分页显示输出

		$this->assign('page',$show);// 赋值分页输出

        //-------------------------固定参数输出-----------------
		$driver =M('driver')->where('type=1')->select();
		$this->assign('driver',$driver);
		$cartype =M('cartype')->where('1=1')->select();
		$this->assign('cartype',$cartype);
        $this->assign('statusArr',$this->statusArr);// 状态
        $this->assign('cartype',M('cartype')->field(['id','title'])->select());// 状态
        

		$this->display('index');

    }

	public function car_edit(){
		if(I('id')){
			$id = I('id',0,'int');
			$car = $this->m->find($id);
			if(!empty($car['worker_id'])){
				$worker_id = explode(',',$car['worker_id']);//随车工人数组
				$this->assign('worker_id',$worker_id);
			}
			$this->assign('car',$car);
		}
		$driver = M('driver')->where(['type'=>1])->select();//司机
		$worker = M('driver')->where(['type'=>2])->select();//司机
		$this->assign('worker',$worker);
		$this->assign('driver',$driver);
		$this->assign('back_url',$_SERVER['HTTP_REFERER']);
		$this->assign('cartypeList',M('cartype')->select());// 车型
		$this->display('car_edit');

    }
    //添加车辆信息
    public function car_save(){
    	$temp = $this->m->create();
    	// ll($temp);die;
    	if(!empty($temp['worker_id'])){
    		foreach($temp['worker_id'] as $k=>$v){
    			$str.=$v.',';
    		}
    		$temp['worker_id'] = rtrim($str,',');
    	}
    	if($temp['car_id']){
    		$res = $this->m->where(['car_id'=>$temp['car_id']])->save($temp);
    	}else{
    		$res = $this->m->add($temp);
    	}
    	if($res){
		    $this->selectPage_ajax( array('act' => '添加', 'back_url' =>U('Car/index'),'jump_url' => U('Car/car_edit')));
		}else{
			 $this->selectPage_ajax( array('act' => '操作未', 'back_url' => U('Car/index'),'jump_url' => U('Car/car_edit')));
		}

    }

	public function save(){
		$temp = $this->m->create();
        $pic_new=I('post.pic_path_new');
        if($pic_new){
            $temp['pic_path']=$this->pic_move($pic_new,$temp['pic_path']);
        }
        // var_dump($temp);die;
		if($temp['id']){
			$this->m->save($temp);
			// $this->selectPage_ajax(array('act' => '修改', 'back_url' => I('back_url'),'jump_url' => U('Driver/edit','id='.$temp['id'])));
			$this->selectPage_ajax(array('act' => '修改', 'back_url' => I('back_url'),'jump_url' => U('Driver/index','id='.$temp['id'])));
		}
		// else{
		//     $result = $this->m->add($temp);
		// 	if($result)
		// 	    $this->selectPage_ajax( array('act' => '添加', 'back_url' => I('back_url'),'jump_url' => U('Driver/edit','area_id='.I('post.area_id'))));
		// 	else $this->error($GLOBALS['notice']['error'][0]);

		// }
	}
	public function driver_save(){
		$temp = $this->m->create();
		$isset = $this->m->where(['username'=>$temp['username']])->find();
		if($isset){
			$this->selectPage_ajax(['act'=>'账号已存在重新输入，操作未']);
		}
		// $temp['pwd'] = md5($temp['pwd']);
		$result = $this->m->add($temp);
		if($result){
		    $this->selectPage_ajax( array('act' => '添加', 'back_url' =>U('Driver/index','id='.$temp['id']),'jump_url' => U('Driver/index','id='.$temp['id'])));
		}else{
			$this->selectPage_ajax(['act'=>'操作未']);
		}
	}
	public function driver_edit(){
		$this->display('driver_edit');
	}
	//车辆损耗填报
	public function loss(){
		$id = I('id');
		$car = $this->m->where(['car_id'=>$id])->find();
		$this->assign('car',$car);
		if(I('loss_id')){
			$loss = M('loss')->where(['loss_id'=>I('loss_id')])->find();
		}else{
			$loss['addtime']=date('Y-m-d',time());	
		}
		$this->assign('loss',$loss);
		$this->display();
	}
	public function loss_save(){
		$data = I('post.');
		// var_dump($data);die;
		$data['amount'] = $data['park_price']+$data['speed_price']+$data['fuel_price']+$data['service_price'];
		if(!empty($data['loss_id'])){
			$res =M('loss')->where(['loss_id'=>$data['loss_id']])->save($data);
			if($res){
		    	$this->selectPage_ajax( array('act' => '编辑', 'back_url' =>U('Car/car_loss',['id'=>$data['car_id']]),'jump_url' => U('Car/loss',['id'=>$data['car_id'],'loss_id'=>$data['loss_id']])));
			}else{
				$this->selectPage_ajax(['act'=>'操作未']);
			}
		}else{
			//新增
			// $data['addtime'] = date('Y-m-d');//当前时间
			$isset=M('loss')->where(['car_id'=>$data['car_id'],'addtime'=>$data['addtime']])->find();
			if(!empty($isset)){
				$this->selectPage_ajax(['act'=>'当日损耗已填报,操作未']);exit;
			}
			$res =M('loss')->add($data);
			if($res){
		    	$this->selectPage_ajax( array('act' => '添加', 'back_url' =>U('Car/index'),'jump_url' => U('Car/loss',['id'=>$data['car_id']])));
			}else{
				$this->selectPage_ajax(['act'=>'操作未']);
			}
		}
	}
	//查看当前车辆损耗
	public function car_loss(){
		$id = I('id');
		$car = $this->m->where(['car_id'=>$id])->find();
		$this->assign('car',$car);
		$list =M('loss')->where(['car_id'=>$id])->select();
		$this->assign('list',$list);
		$this->display();
	}
	public function loss_del(){
		$loss_id = I('loss_id');
		$res= M('loss')->where(['loss_id'=>$loss_id])->delete();
		// $res =1;
		if($res){
			$this->success($GLOBALS['notice']['success'][1]);
		}else{
			$this->error($GLOBALS['notice']['error'][1]);
		}

	}
	//车辆损耗统计
	public function all_loss(){
		$where=[];
		$map=[];
		if(!empty(I('get.start_time')) && !empty(I('get.end_time'))){
			$where['addtime'] = array('between',[I('get.start_time'),I('get.end_time')]);
			$this->assign('start',I('get.start_time'));
			$this->assign('end',I('get.end_time'));
		}
		if(I('car_id')!=''){
			$map['car_id']=I('car_id');
			$this->assign('car_id',I('car_id'));
		}
		//2020/9/27 王鑫要求修改车辆没有损耗置零 但必须显示
		$list = $this->m->field(['car_id','car_no','cartype_id'])->where($map)->select();//车辆列表
		if(!empty($list)){
			foreach ($list as $k => $v) {
				$where['car_id']=array('eq',$v['car_id']);
				$field = array(
					'loss_id',
					'car_id',
					'sum(mile) all_mile',
					'sum(amount) all_amount',
					'sum(park_price) all_park_price',
					'sum(speed_price) all_speed_price',
					'sum(fuel_price) all_fuel_price',
					'sum(service_price) all_service_price',
					'addtime'
				);
				$loss =M('loss')->field($field)->where($where)->find();//抓紧抓紧 心烦不联查了
				if(!empty($loss['loss_id'])){
					$list[$k]['all_mile'] = $loss['all_mile'];
					$list[$k]['all_amount'] = $loss['all_amount'];
					$list[$k]['all_park_price'] = $loss['all_park_price'];
					$list[$k]['all_speed_price'] = $loss['all_speed_price'];
					$list[$k]['all_fuel_price'] = $loss['all_fuel_price'];
					$list[$k]['all_service_price'] = $loss['all_service_price'];
				}else{
					$list[$k]['all_mile'] = 0;
					$list[$k]['all_amount'] = 0;
					$list[$k]['all_park_price'] =0;
					$list[$k]['all_speed_price'] = 0;
					$list[$k]['all_fuel_price'] =0;
					$list[$k]['all_service_price'] =0;
				}
			}
		}
		// ll($list);die;
		//2020/9/27 王鑫要求修改车辆没有损耗置零 但必须显示
		// $field = array(
		// 	'loss_id',
		// 	'car_id',
		// 	'sum(mile) all_mile',
		// 	'sum(amount) all_amount',
		// 	'sum(park_price) all_park_price',
		// 	'sum(speed_price) all_speed_price',
		// 	'sum(fuel_price) all_fuel_price',
		// 	'sum(service_price) all_service_price',
		// 	'addtime'
		// );
		// $list =M('loss')->field($field)->where($where)->order('loss_id desc')->group('car_id')->select();//抓紧抓紧 心烦不联查了
		// if(!empty($list)){
		// 	foreach ($list as $k => $v) {
		// 		$list[$k]['car_no'] = $this->m->where(['car_id'=>$v['car_id']])->getField('car_no');
		// 	}
		// }
		// ll($list);die;
		$car =M('car')->where('1=1')->select();
		$this->assign('car',$car);
		$this->assign('list',$list);
		$this->display();
	}
	public function del(){
		$ids = I('id');
		if($ids){

			if(is_array($ids)){$ids=implode(",",$ids);}

			$this->baseDelFile('cartype',$ids,'pic_path');

			if ($this->m->delete($ids)){

				$ids=explode(",",$ids);

			    $this->success($GLOBALS['notice']['success'][1]);

			}else{

				$this->error($GLOBALS['notice']['error'][1]);

			}

		}else{

			$this->error($GLOBALS['notice']['warming'][1]);

		}		

    }

	public function batch(){          ////批量操作方法



		

		$ids = I('id');

		$val = I('get.val');

		$field = I('get.field');

		

		

		

		if($ids){

			if(is_array($ids)){$ids=implode(",",$ids);}

			

			if ($this->m->where('id in ('.$ids.')')->save(array($field=>$val))){

				$ids=explode(",",$ids);

			    $this->success($GLOBALS['notice']['success'][2]);

			}else{

				$this->error($GLOBALS['notice']['error'][2]);

			}

		}else{

			$this->error($GLOBALS['notice']['warming'][1]);

		}		

    }

	public function ajaxFindNext(){



		$selValue = I('selValue','0','int');

		$areaNextId = I('areaNextId','','int');

		$domId = I('domId');

		$classList = $selValue?$this->m_class->where('parent_id='.$selValue)->select():array();

		$content = "";

		if(!count($classList)){

			echo 0;exit;

		}else{

		

			$content .= '<span id="'.$domId.'_'.$areaNextId.'">'."\n\t";

			$content .= '<select class="form-control input-small" name="class_id[]" onchange="javascript:findNext(this.value , '.$areaNextId.' , \''.$domId.'\')">'."\n\t";

			$content .= '<option value="0">请选择</option>'."\n\t";

			foreach($classList as $item){

			$selectedMark = $selValue==$item['id']?'selected':'';

			$content .= '<option value="'.$item['id'].'" '.$selectedMark.'>'.$item['class_name'].'</option>'."\n\t";

			}

			$content .= '</select>'."\n\t";

			$content .= '</span>'."\n\t";

		

			echo $content;exit;

		

		}

	}

	public function ajaxListInput(){

		header('Content-Type:text/html;charset=utf-8');

		$info = I('post.');

		$data[$info["fieldName"]] = $info["Val"];

		$data["car_id"] = $info["id"];
		// ll($data);die;
		if ($this->m->save($data)){

			echo "success";

		}else{

			echo "error";

		}

		exit;

	}

	

	

	

	

}